package com.client;

import java.util.Vector;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.shared.TaskGrid;
import com.shared.Token;

public class EntryPointMain implements EntryPoint {
  private final ServiceAsync gtiService = GWT.create(Service.class);
  private String code;
  private String tokenAccess;
  private String tokenRefresh;
  private Token  token;

  public void onModuleLoad() {
      
   final String cookieTokenAccessName  = "gtasktokenaccess";
   final String cookieTokenRefreshName = "gtasktokenrefresh";
   code = Window.Location.getParameter("code");
   
   final VerticalPanel sp = new VerticalPanel ();
   RootPanel.get().add(sp);
   
   if (code == null) {
       tokenAccess  = Cookies.getCookie(cookieTokenAccessName);
       tokenRefresh = Cookies.getCookie(cookieTokenRefreshName);
       token        = new Token (tokenAccess, tokenRefresh);
       //sp.add(new HTML(tokenAccess));
       //sp.add(new HTML(tokenRefresh));
       //Window.alert("cookie: " + tokenAccess + "," + tokenRefresh);
       
   }
   else {
       gtiService.authenticate(code, new AsyncCallback<Token>() {
           public void onFailure(Throwable caught) {
             Window.alert(caught.toString());           
           }
           public void onSuccess(Token result) {  
               token = result;
               Cookies.setCookie(cookieTokenAccessName ,token.getAccess());
               Cookies.setCookie(cookieTokenRefreshName,token.getRefresh());
           }
         }); 
   }   

   final Anchor anchor = new Anchor ("renew access");
   anchor.setTarget("_blank");
   sp.add(anchor);

   gtiService.getAuthorizationUrl(new AsyncCallback<String>() {
       public void onFailure(Throwable caught) {
         Window.alert(caught.toString());           
       }
       public void onSuccess(String result) {           
           anchor.setHref(result);
       }
     });   
   
   final FlexTable flexTable = new FlexTable ();  
   Button button = new Button("get all details", new ClickHandler(){        
      @Override
      public void onClick(ClickEvent event) {  
          Cookies.setCookie("gtasktoken", code);
          gtiService.getList(token, new AsyncCallback<Vector<TaskGrid>>() {
              public void onFailure(Throwable caught) {
                Window.alert(caught.toString());           
              }
              public void onSuccess(Vector<TaskGrid> result) {                  
                  flexTable.clear ();                  
                  int row = 1;
                  for (TaskGrid taskGrid : result) {
                      int column = new Integer(taskGrid.getLevel()).intValue();
                      flexTable.setWidget(row,column,new HTML (taskGrid.toString()));
                      row++;
                  }                 
              }
          });      
      }
  });
  sp.add(button);
  sp.add(flexTable);
  }
}
